package com.example.bbs.web.servlet; /**
 * @auther: zhangxin
 * @Description:
 */

import com.example.bbs.model.Block;
import com.example.bbs.model.Post;
import com.example.bbs.model.User;
import com.example.bbs.service.BlockService;
import com.example.bbs.service.PostService;
import com.example.bbs.service.UserService;
import com.example.bbs.service.impl.BlockServiceImpl;
import com.example.bbs.service.impl.PostServiceImpl;
import com.example.bbs.service.impl.UserServiceImpl;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "PostServlet", value = "/bbs/posts")
public class PostServlet extends HttpServlet {
    PostService postService = new PostServiceImpl();
    BlockService blockService = new BlockServiceImpl();
    UserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        int bid = Integer.parseInt(req.getParameter("bid"));
        List<Post> posts = postService.findPostByBlockId(bid);
        Block block = blockService.findBlockById(bid);
        List<User> users = new ArrayList();

        for(int i = 0; i < posts.size(); i++){
            //懒加载
            User user = createByCglib(posts.get(i).getUserId(), User.class);
//            User user = userService.findUserById(posts.get(i).getUserId());
            users.add(user);
        }
        req.setAttribute("posts",posts);
        req.setAttribute("users",users);
        req.setAttribute("block",block);
        req.getRequestDispatcher("/posts.jsp").forward(req,resp);
    }


    private User createByCglib(int uid, Class<User> userClass) {
        Enhancer enhancer = new Enhancer();
        //设置父类
        enhancer.setSuperclass(userClass);
        //设置回调
        enhancer.setCallback(new LazyUserHandler(new UserServiceImpl(), uid));
        //创建代理对象
        return (User) enhancer.create(new Class[]{int.class}, new Object[]{uid});
    }

    /**
     * 处理懒加载用户的逻辑处理类
     */
    class LazyUserHandler implements MethodInterceptor {
        private UserService userService;
        private int uid;

        LazyUserHandler(UserService userService, int uid) {
            this.userService = userService;
            this.uid = uid;
        }

        @Override
        public Object intercept(Object proxy,
                                Method method,
                                Object[] objects,
                                MethodProxy methodProxy) throws Throwable {
//            System.out.println("进入intercept");
            //判断调用的方法
            if (method.getName().startsWith("set")) {
                return methodProxy.invokeSuper(proxy, objects);
            }
            if (method.getName().equals("getUserId")) {
                return methodProxy.invokeSuper(proxy, objects);
            }
            //数据库查询用户信息
            User user = userService.findUserById(uid);
            //调用真实对象的真实方法
            return method.invoke(user, objects);
        }

    }

}
